home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* DATABOSS MODULE: DB_KEY.C */
- /****************************************************************************/
-
- #include <stdio.h>
- #include <conio.h>
- #ifndef __TURBOC__
- #include <graph.h>
- #endif
- #include <dos.h>
- #include <stddef.h>
- #include <string.h>
- #include "db_lsc.h"
- #include "db_types.h"
- #include "db_conio.h"
- #include "db_dos.h"
- #include "db_key.h"
- #include "db_str.h"
-
- /**************************** GLOBAL CONSTANTS ****************************/
-
- const mkey MLeftB = {Enter, False};
- const mkey MRightB = {Esc, False};
- const mkey MBothB = {F1, True};
- const mkey MCenterB = {F1, True};
- const mkey MCLeftB = {F1, True};
- const mkey MCRightB = {F1, True};
- const mkey MAllB = {F1, True};
- const mkey MUp = {UArr, True};
- const mkey MDown = {DArr, True};
- const mkey MLeft = {LArr, True};
- const mkey MRight = {RArr, True};
-
- /*
- const uchar Uxlat1[] = "ÇÜÉâÄàÅÇêëèïîìÄÅÉÆÆôÖòûùÿÖÜ¢£¥₧ƒáíóúÑÑ";
- const uchar Uxlat2[] = "ABΓπΣΣMTΦΘ∞φEH";
- const uchar Lxlat1[] = "çüéâäàåçêëèïîìäåéææôöòûùÿöü¢£¥₧ƒáíóúññ";
- const uchar Lxlat2[] = "αßΓπσσµτΦΘΩδ∞φε∩";
- */
- uchar Uxlate[]=
- "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xA\xB\xC\xD\xE\xF"
- "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
- " !""\x22""#$%&'()*+,-./0123456789:;<=>?"
- "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[""\x5C""]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~""\x7F"
- "ÇÜÉAÄAÅÇEEEIIIÄÅÉÆÆOÖOUUYÖÜ¢£¥₧ƒAIOUÑѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐"
- "└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■""\xFF";
-
- uchar Lxlate[]=
- "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xA\xB\xC\xD\xE\xF"
- "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
- " !""\x22""#$%&'()*+,-./0123456789:;<=>?"
- "@abcdefghijklmnopqrstuvwxyz[""\x5C""]^_`abcdefghijklmnopqrstuvwxyz{|}~""\x7F"
- "çüéâäàåçêëèïîìäåéææôöòûùÿöü¢£¥₧ƒáíóúññªº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐"
- "└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■""\xFF";
-
- const uchar IBMGrLines[] = "│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀";
- const uchar BasicLines[] = "|++++++\"+++++++++-++++++++=+++++++++++++|-||-";
-
- /**************************** GLOBAL VARIABLES ****************************/
-
- bool ehk;
- bool _xkey;
- bool _ckey;
- bool _akey;
- bool mousekey;
- bool moused;
- background ssave;
- bool scrrecurs;
- bool resetdeltas;
- cditype cdi;
- casemapfun casemapupch;
- kpressfunc kpressed;
- getkeyfunc getkey;
-
-
- /*************************** INTERNAL VARIABLES ***************************/
-
- static uchar lastdirn;
- static byteptr keystats = MK_FP(0x0040,0x0017);
- static wordptr mswintraddr = MK_FP(0x0000,0x00CC);
-
- static bool initialized = False;
-
- /***************************** IMPLEMENTATION *****************************/
-
- word getvmode(void)
- {
- union REGS reg;
- byte videoMode, noRows, noCols, theAttr;
- unsigned int font8x8 = 256;
-
- reg.h.ah = 15;
-
- /* swinterrupt(0x10,®,®); */
-
- int86(0x10, ®, ®);
- videoMode = reg.h.al; /* SN 3.5 */
- if (!videoMode) { /* SN 3.5 */
- getscreeninfo(&videoMode, &noCols, &noRows, &theAttr); /* SN 3.5 */
- if (noRows > 24) videoMode = font8x8; /* SN 3.5 */
- } /* SN 3.5 */
- return (videoMode); /* SN 3.5 */
- }
-
- bool printer_ok(byte prt)
- {
- union REGS regs;
-
- if ((prt >=1) && (prt <=3)) prt = 1;
-
- regs.h.ah = 2;
- regs.x.dx = prt - 1;
-
- /* swinterrupt(0x17,®s,®s); */
-
- int86(0x17, ®s, ®s);
- return ((bool) (((regs.h.ah & 0x10) == 0x10) && ((regs.h.ah & 0x20) != 0x20)));
- }
-
- bool mintrloaded(void)
- {
- union REGS regs;
-
- regs.x.ax = 0;
-
- /* swinterrupt(0x33,®s,®s); */
-
- int86(0x33, ®s, ®s);
- return ((bool) (regs.x.ax != 0));
- }
-
- bool mouseinstalled(void)
- {
- return ((bool) ((*mswintraddr == 0) ? False : mintrloaded()));
- }
-
- void setratio(int x, int y)
- {
- union REGS regs;
-
- regs.x.ax = 0x0F;
- regs.x.cx = x;
- regs.x.dx = y;
-
- /* swinterrupt(0x33,®s,®s); */
-
- int86(0x33, ®s, ®s);
- }
-
- void showmouse(void)
- {
- union REGS regs;
-
- regs.x.ax = 1;
-
- /* swinterrupt(0x33,®s,®s); */
-
- int86(0x33, ®s, ®s);
- }
-
- void hidemouse(void)
- {
- union REGS regs;
-
- regs.x.ax = 2;
-
- /* swinterrupt(0x33,®s,®s); */
-
- int86(0x33, ®s, ®s);
- }
-
- void getmouse(mouseb *button, int *column, int *row)
- {
- union REGS regs;
-
- regs.x.ax = 3;
-
- /* swinterrupt(0x33,®s,®s); */
-
- int86(0x33, ®s, ®s);
- *column = ((regs.x.cx + 4) / 8) + 1;
- *row = ((regs.x.dx + 4) / 8) + 1;
- *button = regs.x.bx & 0x07;
- }
-
- void mgotoxy(int col, int row)
- {
- union REGS regs;
-
- regs.x.ax = 4;
- regs.x.cx = (col * 8) - 4 - 1;
- regs.x.dx = (row * 8) - 4 - 1;
-
- /* swinterrupt(0x33,®s,®s); */
-
- int86(0x33, ®s, ®s);
- }
-
- bool mousepressed(int mousep)
- {
- union REGS regs;
-
- regs.x.ax = 5;
- regs.x.bx = mousep;
-
- /* swinterrupt(0x33,®s,®s); */
-
- int86(0x33, ®s, ®s);
- return ((bool) (regs.x.bx > 0));
- }
-
- bool mousereleased(int mousep)
- {
- union REGS regs;
-
- regs.x.ax = 6;
- regs.x.bx = mousep;
-
- /* swinterrupt(0x33,®s,®s); */
-
- int86(0x33, ®s, ®s);
- return ((bool) (regs.x.bx > 0));
- }
-
- void mousehl(int left, int right)
- {
- union REGS regs;
-
- regs.x.ax = 7;
- regs.x.cx = (left * 8) - 1;
- regs.x.dx = (right * 8) - 1;
-
- /* swinterrupt(0x33,®s,®s); */
-
- int86(0x33, ®s, ®s);
- }
-
- void mousevl(int top, int bot)
- {
- union REGS regs;
-
- regs.x.ax = 8;
- regs.x.cx = (top * 8) - 1;
- regs.x.dx = (bot * 8) - 1;
-
- /* swinterrupt(0x33,®s,®s); */
-
- int86(0x33, ®s, ®s);
- }
-
- void mousechar(int ordchar)
- {
- union REGS regs;
-
- regs.x.ax = 10;
- regs.x.bx = 0;
- regs.x.cx = 0x7700;
- regs.x.dx = 0x0077 & ordchar;
-
- /* swinterrupt(0x33,®s,®s); */
-
- int86(0x33, ®s, ®s);
- }
-
- uchar filterch(uchar inch, prtmode pmode)
- {
- uchar fval;
- int p;
-
- if (inch < ' ') fval = ' ';
- else {
- if ((inch > 127) && (pmode == BasicChrs)) {
- p = 0;
- while ((IBMGrLines[p] != '\0') && (IBMGrLines[p] != inch)) p++;
- fval = (IBMGrLines[p] != '\0') ? BasicLines[p] : ' ';
- }
- else fval = inch;
- }
- return (fval);
- }
-
- strptr filterstr(strptr destination, strptr source, prtmode printMode) /* SN 3.5 */
- { /* SN 3.5 */
- strptr aStringPtr; /* SN 3.5 */
- int printPosition; /* SN 3.5 */
- /* SN 3.5 */
- aStringPtr = destination; /* SN 3.5 */
- strcpy(destination, source); /* SN 3.5 */
- if (printMode != AllChrs) { /* SN 3.5 */
- while(*(aStringPtr)) { /* SN 3.5 */
- if (*aStringPtr < (char)0x20) *aStringPtr = (char)0x20; /* SN 3.5 */
- else { /* SN 3.5 */
- if ((*aStringPtr > (char)0x7F) && (printMode == BasicChrs)) { /* SN 3.5 */
- printPosition = (int)strchr(IBMGrLines, *aStringPtr); /* SN 3.5 */
- if (!printPosition) *aStringPtr = *(BasicLines + printPosition); /* SN 3.5 */
- else *aStringPtr = (char)0x20; /* SN 3.5 */
- } /* SN 3.5 */
- } /* SN 3.5 */
- aStringPtr++;
- } /* SN 3.5 */
- } /* SN 3.5 */
- return(destination); /* SN 3.5 */
- } /* SN 3.5 */
-
- uchar upperch(uchar ch)
- {
- if (!_xkey) ch = Uxlate[ch];
- return (ch);
- }
-
- strptr upper(strptr sout, strptr strg)
- {
- strptr sptr;
-
- _xkey = False;
- sptr = sout;
- while (*strg) *sptr++ = upperch(*strg++);
- *sptr = '\0';
- return (sout);
- }
-
- uchar lowerch(uchar ch)
- {
- if (!_xkey) ch = Lxlate[ch];
- return (ch);
- }
-
- strptr lower(strptr sout, strptr strg)
- {
- strptr sptr;
-
- _xkey = False;
- sptr = sout;
- while (*strg) *sptr++ = lowerch(*strg++);
- *sptr = '\0';
- return (sout);
- }
-
- bool detectk(byte key)
- {
- union REGS regs;;
-
- regs.h.ah = 0x02;
-
- /* swinterrupt(0x16,®s,®s); */
-
- int86(0x16, ®s, ®s);
- return ((bool) ((regs.h.al & key) != 0));
- }
-
- bool enhdetectk(byte key)
- {
- union REGS regs;
-
- regs.h.ah = 0x12;
-
- /* swinterrupt(0x16,®s,®s); */
-
- int86(0x16, ®s, ®s);
- return ((bool) ((regs.h.ah & key) != 0));
- }
-
- void setcaplock(bool on)
- {
- if (on) *keystats |= CapsLock;
- else *keystats &= ~CapsLock;
- }
-
- void setnumlock(bool on)
- {
- if (on) *keystats |= NumLock;
- else *keystats &= ~NumLock;
- }
-
- void setscrolllock(bool on)
- {
- if (on) *keystats |= ScrollLock;
- else *keystats &= ~ScrollLock;
- }
-
- strptr keystat(strptr sout, byte mode)
- {
- strcpy(sout,(detectk(CapsLock)) ? msg_CAPS : " "); /* SN 3.5 */
- strcat(sout,(detectk(NumLock)) ? msg_NUM : " "); /* SN 3.5 */
- strcat(sout,(detectk(ScrollLock)) ? msg_SCROLL : " "); /* SN 3.5 */
- return (sout);
- }
-
- void kflush(void)
- {
- union REGS regs;
-
- if (moused) mgotoxy(MouseCH,MouseCV);
- regs.h.ah = 0x0C;
- regs.h.al = 0x06;
- regs.h.dl = 0xFF;
- intdos(®s,®s);
- }
-
- bool internalkpressed(void)
- {
- union REGS regs;
- bool finished;
- mouseb b;
- int col,row;
-
- if (!scrrecurs && (ssave != NULL)) {
- scrrecurs = True;
- (*ssave)();
- scrrecurs = False;
- }
- finished = False;
- if (moused) {
- getmouse((mouseb *)&b,&col,&row);
- if ((b != NoB) || (col != MouseCH) || (row != MouseCV)) finished = True;
- }
- if (!finished) {
- regs.h.ah = (byte) ((ehk) ? 0x11 : 0x01);
-
- /* swinterrupt(0x16,®s,®s); */
-
- #ifdef __TURBOC__
- int86(0x16, ®s, ®s);
- finished = (bool) ((regs.x.flags & 0x0040) == 0);
- #else
- finished = (bool) kbhit();
- #endif
- }
- if (finished) resetdeltas = True;
- return (finished);
- }
-
- uchar internalgetkey(void)
- {
- union REGS regs;
- bool finished,action;
- uchar ch;
- int col,row;
- mouseb b,bt;
- mkey mch;
-
- while (!kpressed());
-
- _xkey = False;
- mousekey = True;
- finished = False;
- action = False;
-
- if (moused) {
- do {
- getmouse((mouseb *)&b,&col,&row);
- if (b != NoB) {
- bt = b;
- delay(50);
- getmouse((mouseb *)&b,&col,&row);
- if (b == NoB) b = bt;
- }
- switch (b) {
- case LeftB : mch = MLeftB; break;
- case RightB : mch = MRightB; break;
- case BothB : mch = MBothB; break;
- case CenterB : mch = MCenterB; break;
- case CLeftB : mch = MCLeftB; break;
- case CRightB : mch = MCRightB; break;
- case AllB : mch = MAllB; break;
- }
- if (b != NoB) {
- finished = True;
- while (b != NoB) getmouse((mouseb *)&b,&col,&row);
- }
- else {
- finished = True;
- if ((lastdirn == MRight.ch) || (lastdirn == MLeft.ch)) {
- if ((col - MouseCH) >= 1) mch = MRight;
- else if ((MouseCH - col) >= 1) mch = MLeft;
- else if ((row - MouseCV) >= 1) mch = MDown;
- else if ((MouseCV - row) >= 1) mch = MUp;
- else finished = False;
- }
- else {
- if ((row - MouseCV) >= 1) mch = MDown;
- else if ((MouseCV - row) >= 1) mch = MUp;
- else if ((col - MouseCH) >= 1) mch = MRight;
- else if ((MouseCH - col) >= 1) mch = MLeft;
- else finished = False;
- }
- }
- moused = False;
- if (kpressed() || finished) action = True;
- moused = True;
- } while (!action);
- }
- if (moused && finished) {
- mgotoxy(MouseCH,MouseCV);
- ch = mch.ch;
- _xkey = mch._xkey;
- lastdirn = ch;
- }
- if (!finished) {
- _ckey = detectk(CtrlKey);
- _akey = detectk(AltKey);
- mousekey = False;
- regs.h.ah = (byte) ((ehk) ? 0x10 : 0x00);
-
- /* swinterrupt(0x16,®s,®s); */
-
- int86(0x16, ®s, ®s);
- if ((regs.h.al == 0x00) || (regs.h.al == 0xE0)) {
- ch = regs.h.ah | 0x80;
- _xkey = True;
- }
- else
- ch = regs.h.al;
- }
- return (ch);
- }
-
- void delaykey(int time)
- {
- int i;
-
- i = 1;
- while (i++ < (time/100)) {
- delay(100);
- if (kpressed()) {
- getkey();
- break;
- }
- }
- }
-
- bool enhkbd(void)
- {
- union REGS regs;
- byte b;
-
- regs.h.ah = 0x02;
-
- /* swinterrupt(0x16,®s,®s); */
-
- int86(0x16, ®s, ®s);
- b = regs.h.al;
- regs.x.ax = 0x1200 + (b ^ 0xFF);
-
- /* swinterrupt(0x16,®s,®s); */
-
- int86(0x16, ®s, ®s);
- return ((bool) (regs.h.al == b));
- }
-
- void chkforkswitch(void)
- {
- int i;
- string ts;
-
- for (i = 1; i < paramcount(); i++) {
- strcpy(ts,paramstr(i));
- if ((*ts == '-') || (*ts == '/')) {
- strdelete(ts,0,1);
- if (strcmpi(ts,"K") == 0) ehk = False;
- }
- }
- }
-
- void initcdi(void)
- {
- union REGS regs;
- struct SREGS sregs;
- ptr p;
- int ch_i;
-
- p = &cdi;
- regs.x.ax = 0x3800;
- regs.x.dx = FP_OFF(p);
- sregs.ds = FP_SEG(p);
- intdosx(®s,®s,&sregs);
-
- /*************************************************************************
- The case map function is provided as a feature of Microsoft DOS. The
- address of the case map function is returned as part of the Country
- Dependant Information block vai a call to Int 21 function 0x38. This
- case map function needs the character to be uppercased to be put into
- the "AL" register, a far call to be placed to the "case_map" address
- after which the upper cased character is returned in th "AL" register.
- The mapping function is only provided for characters >= \x80. If a
- character does not have a corresponding upper case character then the
- contents of "AL" are unchanged.
-
- Use of the "casemapupch" function is dependant upon the implementation
- of the compiler. It assumes that when the "C" compilers detect a
- "char" function with one "char" parameter it will load the parameter
- into the "AL" register and will return the contents of the "AL" register
- as the function result.
- *************************************************************************/
- casemapupch = cdi.case_map;
- for (ch_i = 0x80; ch_i <= 0xFF; ch_i++) Uxlate[ch_i] = casemapupch((char)ch_i);
- }
-
- void db_key_init(void)
- {
- if (!initialized) {
- kpressed = internalkpressed;
- getkey = internalgetkey;
- initialized = True;
- _xkey = _ckey = _akey = False;
- scrrecurs = False;
- ssave = NULL;
- ehk = enhkbd();
- chkforkswitch();
- mousekey = False;
- moused = mouseinstalled();
- if (moused) {
- setratio(40,40);
- mgotoxy(MouseCH,MouseCV);
- }
- initcdi();
- }
- }
-
- /***************************** END OF DB_KEY.C ****************************/
-